package net.cattaka.teamredwhite.web;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.util.Map;

import net.cattaka.teamredwhite.HTTPServer;

import android.content.Context;

public class ContentWriter {
	public static final String PATH_KEY = "path";
	private Context context;
	
	public ContentWriter(Context context) {
		super();
		this.context = context;
	}

//	public void handle(String path, Map<String, String> params, InputStream in, OutputStream out) throws IOException {
//		AbstractContent abstractContent = null;
//		
//		if ("/".equals(path) || "/index.html".equals(path)) {
//			AssetsContent assetsContent = new AssetsContent();
//			assetsContent.setFile("index.html");
//			assetsContent.setMime("text/html");
//			abstractContent = assetsContent;
//		} else if ("/imagelist.dat".equals(path)) {
//			ImageListContent imageListContent = new ImageListContent();
//			imageListContent.setPath(IMAGE_PATH);
//			imageListContent.setMime("application/json");
//			abstractContent = imageListContent;
//		} else if (path != null && path.startsWith("/images/")) {
//			ImageContent imageContent = new ImageContent();
//			imageContent.setFile(path.substring("/images/".length()));
//			abstractContent = imageContent;
//		}
//		
//		if (abstractContent != null) {
//			abstractContent.setContext(context);
//			abstractContent.handle(null, params, in, out);
//		} else {
//			outputResponseHeader(out, 400, "text/plain", 0);
//		}
//		out.flush();
//	}
	
	public static void outputResponseHeader(OutputStream out, int status, String mime, int contentLength) throws IOException {
		out.write(("HTTP/1.1 " + status + " OK\n").getBytes());
		out.write("Server: TeamReadWhite\n".getBytes());
		out.write("Connection: close\n".getBytes());
		if (contentLength >= 0) {
			out.write(("Content-Length: " + contentLength + "\n").getBytes());
		}
		if (mime != null) {
			out.write(("Content-Type: " + mime + "\n").getBytes());
		}
		out.write(("\n").getBytes());
	}
}
